package com.laivi.basic.module.log;

import java.util.Date;

import com.laivi.basic.util.DateUtil;

/**
 * @author laivi.zhu@gmail.com
 * @date 2014-7-2
 * description:
 */
public class SystemLogger extends AbstractLogger {
	private Class<?> klass;
	
	public SystemLogger(Class<?> klass) {
		super();
		this.klass = klass;
	}

	@Override
	public void info(String msg) {
		StackTraceElement[] stackElements=new Throwable().getStackTrace();
		System.out.println("INFO \t"+getDate()+" \t"+klass.getName()+":"+stackElements[1].getLineNumber()+" \t--"+msg);
	}

	@Override
	public void warn(String msg) {
		StackTraceElement[] stackElement=new Throwable().getStackTrace();
		System.out.println("WARN \t"+getDate()+" \t"+klass.getName()+":"+stackElement[1].getLineNumber()+" \t--"+msg);
	}

	@Override
	public void error(String msg) {
		StackTraceElement[] stackElement=new Throwable().getStackTrace();
		System.out.println("ERROR \t"+getDate()+" \t"+klass.getName()+":"+stackElement[1].getLineNumber()+" \t--"+msg);
	}

	@Override
	public void error(String msg, Throwable cause) {
		StringBuffer sb=new StringBuffer(msg+":"+cause.getMessage()+"\n");
		for(StackTraceElement stackElement: cause.getStackTrace()){
			sb.append("\t"+stackElement+"\n");
		}
		error(sb.toString());
	}
	
	public String getDate(){
		return DateUtil.format(new Date(), DateUtil.DAY_TIME+":SSS");
	}

	@Override
	public void error(Throwable cause) {
		
	}

}
